﻿using NB.MGJ.Common.MVC;
using NB.MGJ.Model.HomeCount;
using NB.MGJ.Model.HomeIndexViewModel;
using NB.MGJ.Service.AntiFake;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace NB.MGJ.Web.Controllers
{
    public class HomeDisplayController : BaseController
    {
        //
        // GET: /HomeDisplay/


        AntiFakeQueryCodeLogService _AntiFakeQueryCodeLogService;

        public HomeDisplayController()
        {
            _AntiFakeQueryCodeLogService = new AntiFakeQueryCodeLogService();
        }

        /// <summary>
        /// 获取今日昨日分时数据
        /// </summary>
        /// <returns></returns>
        public ActionResult GetLastDayAndTodayData()
        {
            #region 测试数据

            var One = 0;
            var Two = 23;
            var Three = 67;
            var Four = 78;
            var Five = 78;

            int[] sectionDayCheck = { One, Two, Three, Four, Five };
            int[] sectionYesterdayCheck = { One + 1, Two + 2, Three + 3, Four + 4, Five + 5 };

            #endregion

            HourParmModel hpModel1 = new HourParmModel()
            {
                Parm = 1,
                Start = "00:00:00",
                End = "05:59:59"

            };

            HourParmModel hpModel2 = new HourParmModel()
            {
                Parm = 1,
                Start = "06:00:00",
                End = "11:59:59"

            };
            HourParmModel hpModel3 = new HourParmModel()
            {
                Parm = 1,
                Start = "12:00:00",
                End = "17:59:59"

            };
            HourParmModel hpModel4 = new HourParmModel()
            {
                Parm = 1,
                Start = "18:00:00",
                End = "23:59:59"

            };

            List<HourParmModel> hpList = new List<HourParmModel>();

            hpList.Add(hpModel1);
            hpList.Add(hpModel2);
            hpList.Add(hpModel3);
            hpList.Add(hpModel4);


            var ECode = LogOnUser.ECode;

            var today = _AntiFakeQueryCodeLogService.GetHourDataByDayAndCode(DateTime.Now, hpList, ECode);
            var yesterday = _AntiFakeQueryCodeLogService.GetHourDataByDayAndCode(DateTime.Now.AddDays(-1), hpList, ECode);

            int[] _sectionDayCheck = { 0, Convert.ToInt32(today.One), Convert.ToInt32(today.Two), Convert.ToInt32(today.Three), Convert.ToInt32(today.Four) };
            int[] _sectionYesterdayCheck = { 0, Convert.ToInt32(yesterday.One), Convert.ToInt32(yesterday.Two), Convert.ToInt32(yesterday.Three), Convert.ToInt32(yesterday.Four) };

            Hashtable ht = new Hashtable();
            ht.Add("sectionDayCheck", _sectionDayCheck);
            ht.Add("sectionYesterdayCheck", _sectionYesterdayCheck);

            var AlreadyCreateCodeCount = _AntiFakeQueryCodeLogService.GetAlreadyCreateCodeCount();

            double  AlreadyCreateCodeCountWan=Math.Round( (double)(AlreadyCreateCodeCount/10000),2);
            var  AlreadyScanCodeCount=_AntiFakeQueryCodeLogService.GetAlreadyScanCodeCount(LogOnUser.ECode);
            var EnterpriseCount=_AntiFakeQueryCodeLogService.GetEnterpriseCount();
            var ToDayScanCodeCount = _AntiFakeQueryCodeLogService.GetToDayScanCodeCount(LogOnUser.ECode);

            ht.Add("AlreadyCreateCodeCount", AlreadyCreateCodeCountWan);
            ht.Add("AlreadyScanCodeCount",AlreadyScanCodeCount);
            ht.Add("EnterpriseCount",EnterpriseCount);
            ht.Add("ToDayScanCodeCount",ToDayScanCodeCount);

            return Json(ht);
        }


        public ActionResult GetLogRate()
        {
            //var list = _AntiFakeQueryCodeLogService.GetLogByDateAndECode(DateTime.Now, LogOnUser.ECode);

            var queryTypeRateModel = _AntiFakeQueryCodeLogService.GetQueryTypeRate(DateTime.Now, LogOnUser.ECode);

            var queryTypeCountModel1 = new QueryTypeCountModel();
            queryTypeCountModel1.type = 0;
            queryTypeCountModel1.QueryRate = Math.Round((((double)queryTypeRateModel.one) / ((double)queryTypeRateModel.ETcount)) * 100, 6);
            queryTypeCountModel1.ErrorRate = Math.Round((((double)(Convert.ToInt32(queryTypeRateModel.oneerror))) / ((double)queryTypeRateModel.ETcount)) * 100, 6);
            queryTypeCountModel1.AccuracyRate = 1 - queryTypeCountModel1.ErrorRate;


            var queryTypeCountModel2 = new QueryTypeCountModel();
            queryTypeCountModel2.type = 1;
            queryTypeCountModel2.QueryRate = Math.Round((((double)queryTypeRateModel.two) / ((double)queryTypeRateModel.ETcount)) * 100, 6);
            queryTypeCountModel2.ErrorRate = Math.Round((((double)(Convert.ToInt32(queryTypeRateModel.twoerror))) / ((double)queryTypeRateModel.ETcount)) * 100, 6);
            queryTypeCountModel2.AccuracyRate = 1 - queryTypeCountModel2.ErrorRate;

            var queryTypeCountModel3 = new QueryTypeCountModel();
            queryTypeCountModel3.type = 2;
            queryTypeCountModel3.QueryRate = Math.Round((((double)queryTypeRateModel.three) / ((double)queryTypeRateModel.ETcount)) * 100, 6);
            queryTypeCountModel3.ErrorRate = Math.Round((((double)(Convert.ToInt32(queryTypeRateModel.threeerror))) / ((double)queryTypeRateModel.ETcount)) * 100, 6);
            queryTypeCountModel3.AccuracyRate = 1 - queryTypeCountModel3.ErrorRate;

            var queryTypeCountModel4 = new QueryTypeCountModel();
            queryTypeCountModel4.type = 3;
            queryTypeCountModel4.QueryRate = Math.Round((((double)queryTypeRateModel.four) / ((double)queryTypeRateModel.ETcount)) * 100, 6);
            queryTypeCountModel4.ErrorRate = Math.Round((((double)(Convert.ToInt32(queryTypeRateModel.fourerror))) / ((double)queryTypeRateModel.ETcount)) * 100, 6);
            queryTypeCountModel4.AccuracyRate = 1 - queryTypeCountModel4.ErrorRate;


            List<QueryTypeCountModel> list = new List<QueryTypeCountModel>();

            list.Add(queryTypeCountModel1);
            list.Add(queryTypeCountModel2);
            list.Add(queryTypeCountModel3);
            list.Add(queryTypeCountModel4);

            return Json(list);
        }

        public ActionResult GetLastSevenDaysData()
        {


            string[] _sevenDays = { "1", "2", "3", "4", "5", "6", "7" };
            for (int i = 6; i >= 0; i--)
            {
                _sevenDays[i] = DateTime.Now.AddDays(-i).ToString("yyyy-MM-dd");
            }


            var antifake = _AntiFakeQueryCodeLogService.GetLastSevenDayDataByType(_sevenDays, 0, LogOnUser.ECode);
            var origins = _AntiFakeQueryCodeLogService.GetLastSevenDayDataByType(_sevenDays, 1, LogOnUser.ECode);
            var activity = _AntiFakeQueryCodeLogService.GetLastSevenDayDataByType(_sevenDays, 2, LogOnUser.ECode);
            var ch = _AntiFakeQueryCodeLogService.GetLastSevenDayDataByType(_sevenDays, 3, LogOnUser.ECode);


            long[] _antifake = { antifake.First, antifake.Second, antifake.Thrid, antifake.Fourth, antifake.Fifth, antifake.Sixth, antifake.Seventh };

            long[] _origins = { origins.First, origins.Second, origins.Thrid, origins.Fourth, origins.Fifth, origins.Sixth, origins.Seventh };

            long[] _activity = { activity.First, activity.Second, activity.Thrid, activity.Fourth, activity.Fifth, activity.Sixth, activity.Seventh };

            long[] _ch = { ch.First, ch.Second, ch.Thrid, ch.Fourth, ch.Fifth, ch.Sixth, ch.Seventh };


            Hashtable ht = new Hashtable();
            ht.Add("sevenDays", _sevenDays);
            ht.Add("antifake", _antifake);
            ht.Add("origins", _origins);
            ht.Add("activity", _activity);
            ht.Add("ch", _ch);
            return Json(ht);
        }



        public ActionResult GetRealTimelogList()
        {
          
            var list = _AntiFakeQueryCodeLogService.GetLogByDateAndECode(DateTime.Now, LogOnUser.ECode, 20);
            return Json(list);
        }


        public ActionResult GetRealTimeLogListAdmin()
        {
            return Json(_AntiFakeQueryCodeLogService.GetRealTimeLogListAdmin_New(), JsonRequestBehavior.AllowGet);
        }



        public ActionResult GetSearchCount()
        {
            var queryTypeRateModel = _AntiFakeQueryCodeLogService.GetQueryTypeRate(DateTime.Now, LogOnUser.ECode);
            return Json(queryTypeRateModel);
        }

        public ActionResult GetProductSearch()
        {
            var list = _AntiFakeQueryCodeLogService.GetProductSearch(LogOnUser.ECode, 6);

            return Json(list);
        }

        public ActionResult GetProvinceData()
        {

            var antifakelist = _AntiFakeQueryCodeLogService.GetProvinceData(LogOnUser.ECode, 0);
            var originslist = _AntiFakeQueryCodeLogService.GetProvinceData(LogOnUser.ECode, 1);
            var activitylist = _AntiFakeQueryCodeLogService.GetProvinceData(LogOnUser.ECode, 2);
            var chlist = _AntiFakeQueryCodeLogService.GetProvinceData(LogOnUser.ECode, 3);

            Hashtable ht = new Hashtable();
            ht.Add("antifakelist", antifakelist);
            ht.Add("originslist", originslist);
            ht.Add("activitylist", activitylist);
            ht.Add("chlist", chlist);
            return Json(ht);

        }

        public ActionResult GetAgeParm()
        {
            HourParmModel hpModel1 = new HourParmModel()
            {
                Parm = 1,
                Start = "0",
                End = "19"
            };

            HourParmModel hpModel2 = new HourParmModel()
            {
                Parm = 2,
                Start = "20",
                End = "29"
            };
            HourParmModel hpModel3 = new HourParmModel()
            {
                Parm = 3,
                Start = "30",
                End = "39"
            };
            HourParmModel hpModel4 = new HourParmModel()
            {
                Parm = 4,
                Start = "40",
                End = "49"
            };

            HourParmModel hpModel5 = new HourParmModel()
            {
                Parm = 4,
                Start = "50",
                End = "150"
            };

            List<HourParmModel> hpList = new List<HourParmModel>();

            hpList.Add(hpModel1);
            hpList.Add(hpModel2);
            hpList.Add(hpModel3);
            hpList.Add(hpModel4);
            hpList.Add(hpModel5);


            var antifake = _AntiFakeQueryCodeLogService.GetAgeParm(hpList, 0, LogOnUser.ECode);
            var origins = _AntiFakeQueryCodeLogService.GetAgeParm(hpList, 1, LogOnUser.ECode);
            var activity = _AntiFakeQueryCodeLogService.GetAgeParm(hpList, 2, LogOnUser.ECode);
            var ch = _AntiFakeQueryCodeLogService.GetAgeParm(hpList, 3, LogOnUser.ECode);

            Hashtable ht = new Hashtable();
            ht.Add("antifake", antifake);
            ht.Add("origins", origins);
            ht.Add("activity", activity);
            ht.Add("ch", ch);

            return Json(ht);

        }

        public ActionResult getSexData()
        {
            var antifake = _AntiFakeQueryCodeLogService.GetSexData(0, LogOnUser.ECode);
            antifake.maleRate = Math.Round((double)(((double)(antifake.maleCount)) / ((double)(antifake.ETcount == 0 ? antifake.ETcount = 100 : antifake.ETcount))), 3);

            antifake.femaleRate = Math.Round((double)(((double)(antifake.femaleCount)) / ((double)(antifake.ETcount == 0 ? antifake.ETcount = 100 : antifake.ETcount))), 3);



            var origins = _AntiFakeQueryCodeLogService.GetSexData(1, LogOnUser.ECode);
            origins.maleRate = Math.Round((double)(((double)(origins.maleCount)) / ((double)(origins.ETcount == 0 ? origins.ETcount = 100 : origins.ETcount))), 3);

            origins.femaleRate = Math.Round((double)(((double)(origins.femaleCount)) / ((double)(origins.ETcount == 0 ? origins.ETcount = 100 : origins.ETcount))), 3);


            var activity = _AntiFakeQueryCodeLogService.GetSexData(2, LogOnUser.ECode);
            activity.maleRate = Math.Round((double)(((double)(activity.maleCount)) / ((double)(activity.ETcount == 0 ? activity.ETcount = 100 : activity.ETcount))), 3);

            activity.femaleRate = Math.Round((double)(((double)(activity.femaleCount)) / ((double)(activity.ETcount == 0 ? activity.ETcount = 100 : activity.ETcount))), 3);


            var ch = _AntiFakeQueryCodeLogService.GetSexData(3, LogOnUser.ECode);
            ch.maleRate = Math.Round((double)(((double)(ch.maleCount)) / ((double)(ch.ETcount == 0 ? ch.ETcount = 100 : ch.ETcount))), 3);

            ch.femaleRate = Math.Round((double)(((double)(ch.femaleCount)) / ((double)(ch.ETcount == 0 ? ch.ETcount = 100 : ch.ETcount))), 3);


            Hashtable ht = new Hashtable();
            ht.Add("antifake", antifake);
            ht.Add("origins", origins);
            ht.Add("activity", activity);
            ht.Add("ch", ch);

            return Json(ht);
        }








	}
}